/*
 * Simple Editor to view contents
 *
 * Created on 1. August 2007, 13:49
 */

package com.google.cse.emea.universaltransformer.editors;

import com.google.cse.emea.jct.utils.Profile;
import com.google.cse.emea.universaltransformer.*;
import java.io.File;
import java.io.IOException;


/**
 * Simple stupid editor to show file contents
 * @author  jtrapp
 */
public class SimpleTableEditor extends javax.swing.JFrame implements EditorInterface{
    private static SimpleTableEditor simpleEditor=null;
    public static SimpleTableEditor getEditor(){
        if (simpleEditor == null){
            simpleEditor = new SimpleTableEditor();
        }
        return simpleEditor;
    }


    /** Creates new form XMLEditor */
    public SimpleTableEditor() {
        initComponents();
    }

    /** Set the file to show.
     * @param file File to show in editor
     * @param charset Character Set used while reading the file contents
     * @throws IOException Some IO Error
     * @throws BadLocationException Some Editor Error
     */
    public void viewFile(String filename, String delimiter, String type, String encoding) throws IOException {
        this.setTitle(filename);
        CSVTableModel model = new CSVTableModel(filename,Integer.MAX_VALUE, delimiter,encoding);
        table.setModel(model);
        this.setVisible(true);
        statusLabel.setText("Size: "+new File(filename).length()+"  Number of Lines: "+model.getRowCount());
                
    }

    
/** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        table = new org.jdesktop.swingx.JXTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        statusLabel = new javax.swing.JLabel();

        jButton1.setText("close");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeEvent(evt);
            }
        });

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(table);

        jScrollPane1.setViewportView(statusLabel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void closeEvent(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeEvent
    this.setVisible(false);
}//GEN-LAST:event_closeEvent

    /**
     *  Standalone version of the simple editor.
     * For test only, becausethe editor is really stupid.
     * @param args the command line arguments
     */
    public static void main(final String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                SimpleTableEditor editor = new SimpleTableEditor();
                editor.setVisible(true);
                if (args.length > 0) {
                    try {
                        editor.viewFile(args[0], "\t", "text/csv", "UTF-16LE");
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel statusLabel;
    private org.jdesktop.swingx.JXTable table;
    // End of variables declaration//GEN-END:variables

    @Override
    public void viewFile(Profile profile, String prefix, String type) throws IOException {
        String inputFile = profile.get(prefix+"file");
        String delimiter =  profile.get(prefix+"delimiter");
        String encoding = profile.get(prefix+"encoding");
        viewFile(inputFile, delimiter, type, encoding);
    }

    @Override
    public String[] getTypes() {
        return new String[] {"text/csv"};
    }
}
